package com.magesti.servlet;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.magesti.DAO.DaoFactory;
import com.magesti.DAOinterface.HistoriqueInterfaceDAO;
import com.magesti.DAOinterface.InterfaceGestionMail;
import com.magesti.DAOinterface.OperationInterfaceDAO;
import com.magesti.beans.Operation;
import com.magesti.beans.Utilisateur;
import com.magesti.form.CreeOperationForm;
import com.magesti.gestionnaire.GestionRole;
import com.magesti.ressource.Generated;
import com.magesti.ressource.Global;
import com.magesti.ressource.ServletGlobal;

//Cette servlet sert de prototye pour creer de nouveaux servlet !
@SuppressWarnings("serial")
public class CreeOperation extends HttpServlet {

	private OperationInterfaceDAO operationDAO;
	private HistoriqueInterfaceDAO historiqueDAO;
	private InterfaceGestionMail gestionnaireMail;

	public void init() throws ServletException {
		DaoFactory f = ((DaoFactory) getServletContext().getAttribute(
				Global.ATT_CONTEXT_DAO_FACTORY));
		operationDAO = f.getOperationDAO();
		historiqueDAO = f.getHistoriqueDAO();
		gestionnaireMail = f.getGestionnaireMail();
	}

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		
		request.setAttribute(Global.ATT_REQUEST_LISTE_DGA_DEMANDEUSE,
				Generated.VALEURS_DGA_DEMANDEUSE);
		ServletGlobal.setRightsUser(request);
		
		this.getServletContext()
				.getRequestDispatcher(Global.VUE_JSP_CREE_OPERATION)
				.forward(request, response);
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		request.setAttribute(Global.ATT_REQUEST_LISTE_DGA_DEMANDEUSE,
				Generated.VALEURS_DGA_DEMANDEUSE);

		CreeOperationForm form = new CreeOperationForm(operationDAO,
				historiqueDAO,gestionnaireMail);
		Operation op = form.genererOperation(request);

		request.setAttribute(Global.ATT_REQUEST_OPERATION, op);
		request.setAttribute(Global.ATT_REQUEST_FORM, form);
		ServletGlobal.setRightsUser(request);
		
		if(form.getErreurs().isEmpty()){
			this.getServletContext()
			.getRequestDispatcher(Global.VUE_JSP_AFFICHE_OPERATION)
			.forward(request, response);
		}else{
		this.getServletContext()
				.getRequestDispatcher(Global.VUE_JSP_CREE_OPERATION)
				.forward(request, response);
		}
	}
	


}
